package org.tigris.atlas.jmx.weblogic;

import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import weblogic.management.MBeanHome;
import weblogic.management.WebLogicMBean;
import weblogic.management.configuration.TargetMBean;

/**
 * Configures a Weblogic JDBC Connection Pool MBean
 */
public class JDBCConnectionPoolConfiguation extends	AbstractResourceConfiguation {
	
	static final String ATTRIBUTE_PROPERTIES = "Properties";
	static final String ATTRIBUTE_SERVER_NAME = "serverName";
	static final String ATTRIBUTE_SID = "SID";
	static final String ATTRIBUTE_USER = "user";
	static final String ATTRIBUTE_TEST_TABLE_NAME = "TestTableName";
	static final String ATTRIBUTE_URL = "URL";
	static final String ATTRIBUTE_SUPPORTS_LOCAL_TRANSACTION = "SupportsLocalTransaction";
	static final String ATTRIBUTE_TEST_CONNECTIONS_ON_RESERVE = "TestConnectionsOnReserve";
	static final String ATTRIBUTE_TEST_CONNECTIONS_ON_RELEASE = "TestConnectionsOnRelease";
	static final String ATTRIBUTE_SHRINKING_ENABLED = "ShrinkingEnabled";
	static final String ATTRIBUTE_SHRINK_PERIOD_MINUTES = "ShrinkPeriodMinutes";
	static final String ATTRIBUTE_REFRESH_MINUTES = "RefreshMinutes";
	static final String ATTRIBUTE_PASSWORD = "Password";
	static final String ATTRIBUTE_DRIVER_NAME = "DriverName";
	static final String ATTRIBUTE_MAX_CAPACITY = "MaxCapacity";
	static final String ATTRIBUTE_INITIAL_CAPACITY = "InitialCapacity";
	static final String ATTRIBUTE_CAPACITY_INCREMENT = "CapacityIncrement";
	static final String CONNECTION_POOL_NAME = "connection.pool.name";
	static final String JDBC_TEST_TABLE_NAME = "jdbc.test.table.name";
	static final String JDBC_URL = "jdbc.url";
	static final String JDBC_SERVER_NAME = "jdbc.server.name";
	static final String JDBC_DATABASE_NAME = "jdbc.database.name";
	static final String JDBC_PASSWORD = "jdbc.password";
	static final String JDBC_USERNAME = "jdbc.username";
	static final String JDBC_DRIVER_NAME = "jdbc.driver.name";
	static final String JDBC_CONNECTION_POOL = "JDBCConnectionPool";
	
	private Log log = LogFactory.getLog(JDBCConnectionPoolConfiguation.class);
	
	/**
	 * Creates a new instance of this configuration object
	 * @param mBeanName The name of the resource to manage
	 * @param properties The properties from which to manage the resorce
	 */
	public JDBCConnectionPoolConfiguation(String mBeanName, Properties properties) {
		super(mBeanName, properties);
	}

	/**
	 * @seeorg.tigris.atlas.jmx.weblogic.AbstractResourceConfiguation#getMBeanType()
	 */
	protected String getMBeanType() {		
		return JDBC_CONNECTION_POOL;
	}
	
	/**
	 * @seeorg.tigris.atlas.jmx.weblogic.AbstractResourceConfiguation#setMBeanAttributes(weblogic.management.configuration.TargetMBean[], weblogic.management.WebLogicMBean, weblogic.management.MBeanHome)
	 */
	protected void setMBeanAttributes(TargetMBean[] targetServers, WebLogicMBean mBean, MBeanHome home) {
		String driverName = getRequiredProperty(JDBC_DRIVER_NAME);	
		String username = getRequiredProperty(JDBC_USERNAME);
		String password = getRequiredProperty(JDBC_PASSWORD);
		String databaseName = getRequiredProperty(JDBC_DATABASE_NAME);
		String serverName = getRequiredProperty(JDBC_SERVER_NAME);
		String url = getRequiredProperty(JDBC_URL);
		String testTableName = getOptionalProperty(JDBC_TEST_TABLE_NAME);
		
		if (!isMissingRequiredProperties()) {
			
			addAttribute(ATTRIBUTE_CAPACITY_INCREMENT, new Integer(1), mBean);
			addAttribute(ATTRIBUTE_INITIAL_CAPACITY, new Integer(1), mBean);
			addAttribute(ATTRIBUTE_MAX_CAPACITY, new Integer(10), mBean);						
			addAttribute(ATTRIBUTE_DRIVER_NAME, driverName, mBean);
			addAttribute(ATTRIBUTE_PASSWORD, password, mBean);
			addAttribute(ATTRIBUTE_REFRESH_MINUTES, new Integer(0), mBean);
			addAttribute(ATTRIBUTE_SHRINK_PERIOD_MINUTES, new Integer(15), mBean);
			addAttribute(ATTRIBUTE_SHRINKING_ENABLED, Boolean.TRUE, mBean);
			addAttribute(ATTRIBUTE_TEST_CONNECTIONS_ON_RELEASE, Boolean.TRUE, mBean);
			addAttribute(ATTRIBUTE_TEST_CONNECTIONS_ON_RESERVE, Boolean.TRUE, mBean);
			addAttribute(ATTRIBUTE_SUPPORTS_LOCAL_TRANSACTION, Boolean.TRUE, mBean);
			addAttribute(ATTRIBUTE_URL, url, mBean);
			addAttribute(ATTRIBUTE_TEST_TABLE_NAME, testTableName, mBean);			
									
			Properties properties = new Properties();
			properties.put(ATTRIBUTE_USER, username);
			properties.put(ATTRIBUTE_SID, databaseName);
			properties.put(ATTRIBUTE_SERVER_NAME, serverName);
			addAttribute(ATTRIBUTE_PROPERTIES, properties, mBean);
			
			//set targets:
			targetToServers(targetServers, mBean);
			
		}
		
	}

	/**
	 * @seeorg.tigris.atlas.jmx.weblogic.AbstractResourceConfiguation#getLog()
	 */
	protected Log getLog() {
		return log;
	}	
	
}
